package com.yunxiao.cloud.sharding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunxiao.cloud.sharding.entity.QueryParam;
import com.yunxiao.cloud.sharding.entity.User;
import com.yunxiao.cloud.sharding.mapper.UserMapper;
import com.yunxiao.cloud.sharding.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: UserServiceImpl
 * @date: 2019/12/25 13:18
 * @author: yunxiao
 * @version: 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<User> queryPage(QueryParam param) {
        return this.page(
                new Page<>(param.getPageNo(), param.getPageSize()),
                new QueryWrapper<User>().like(StringUtils.isNotBlank(param.getUserName()), "user_name", param.getUserName())
                .eq(param.getStatus() != null, "status", param.getStatus())
        );
    }

    @Override
    public PageInfo<User> pageHelper(QueryParam param) {
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<User> userList = userMapper.selectByParam(param);
        return new PageInfo<>(userList);
    }
}
